﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dimebrain.TweetSharp.Extensions;
using Dimebrain.TweetSharp.Fluent;
using Dimebrain.TweetSharp.Model;
using System.Drawing.Drawing2D;
using System.Text.RegularExpressions;


namespace TwitterTestApp
{
    public partial class Message : UserControl
    {

        private Color originalColor = Color.White;

        private Form1 form1;
        public Form1 Form1
        {
            get { return form1; }
            set { form1 = value; }
        }

        //private TwitterStatus status;
        private DirectMessage dmessage;

        //public DirectMessage Dmessage
        //{
        //    get { return dmessage; }
        //    set { dmessage = value; }
        //}

        //public TwitterStatus Status
        //{
        //    get { return status; }
        //}

        public Message()
        {
            InitializeComponent();
            doGraphics();
        }

        ///// <summary>
        ///// Creates a new message control with given message status
        ///// </summary>
        ///// <param name="stat">TwitterStatus status message to display</param>
        //public Message(TwitterStatus stat)
        //    : this()
        //{
        //    this.status = stat;
        //    this.parseMessage(this.status.Text);
        //    this.BackColor = Color.LimeGreen;
        //    this.label1.Text = this.status.User.Name;
        //    this.messageTextBox.Text = this.status.Text;
        //    this.messageBoxThumbnail.ImageLocation = this.status.User.ProfileImageUrl;
        //}

        /// <summary>
        /// Creates new message object to display given direct message
        /// </summary>
        /// <param name="dmessage">DirectMessage to display</param>
        public Message(DirectMessage dm)
            : this()
        {
            this.dmessage = dm;
            changeBorderColor(dm.MessageTypes);
            this.label1.Text = this.dmessage.TDM.Sender.Name;
            this.messageTextBox.Text = this.dmessage.TDM.Text;
            messageBoxThumbnail.ImageLocation = this.dmessage.TDM.Sender.ProfileImageUrl;
        }

        private void changeBorderColor(DirectMessage.Types types)
        {
            if ((types & DirectMessage.Types.Task) != 0)
            { // This is a task
                //tasksPicture.BackgroundImage =    //((System.Drawing.Image)(resources.GetObject("logoPictureBox.Image")));
                tasksPicture.BackColor = Color.Red;
                //this.BackColor = Color.Brown;
            }
            if (((types & DirectMessage.Types.Unread) != 0) &&
                ((types & DirectMessage.Types.Urgent) == 0)){
                // unread msg
                this.BackColor = Color.Purple;
            }
            else if (((types & DirectMessage.Types.Unread) == 0) &&
                ((types & DirectMessage.Types.Urgent) == 0)) {
                // read msg
                    this.BackColor = Color.Plum;
            }
            else if (((types & DirectMessage.Types.Unread) != 0) &&
                ((types & DirectMessage.Types.Urgent) != 0)) {
                // unread urgent
                    this.BackColor = Color.Red;
            }
            else if (((types & DirectMessage.Types.Unread) == 0) &&
                ((types & DirectMessage.Types.Urgent) != 0)) {
                // read urgent
                    this.BackColor = Color.Brown;
            }
        }

        /// <summary>
        /// Creates new message object to display given username and message
        /// </summary>
        /// <param name="username"></param>
        /// <param name="message"></param>
        public Message(string username, string message)
            : this()
        {
            this.label1.Text = username;
            this.messageTextBox.Text = message;
        }

        private void doGraphics()
        {

            Graphics myg = this.CreateGraphics();
            SolidBrush mybrush = new SolidBrush(Color.BlueViolet);
            //Pen mypen = new Pen(mybrush);
            Pen mypen = new Pen(Color.BlueViolet);
            this.Region = new Region(RoundedCornerRectangle(myg, mypen, 0, 0, Width, Height, 20));

            this.panel1.Region = new Region(RoundedCornerRectangle(myg, mypen, 0, 0, panel1.Width, panel1.Height, 20));
        }

        private void message_click(object sender, MouseEventArgs e)
        {
            this.dmessage.IsNew = false;
            form1.displayMessage(this.dmessage);
        }



        public GraphicsPath RoundedCornerRectangle(Graphics gfxObj, Pen penObj, float X, float Y, float RectWidth, float RectHeight, float CornerRadius)
        {

            GraphicsPath gfxPath = new GraphicsPath();

            gfxPath.AddLine(X, Y, X + RectWidth - CornerRadius, Y);//top line
            gfxPath.AddArc(X + RectWidth - (CornerRadius * 2), Y, CornerRadius * 2, CornerRadius * 2, 270, 90);//top right arc
            gfxPath.AddLine(X + RectWidth, Y + CornerRadius, X + RectWidth, Y + RectHeight);//right line
            gfxPath.AddLine(X + RectWidth, Y + RectHeight, X + CornerRadius, Y + RectHeight);//bottom line
            gfxPath.AddArc(X, Y + RectHeight - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 90, 90);//bottom left arc
            gfxPath.AddLine(X, Y + RectHeight - (CornerRadius * 2), X, Y);//left line
            gfxPath.CloseFigure();


            /*gfxPath.AddLine(X + CornerRadius, Y, X + RectWidth - (CornerRadius * 2), Y);
            gfxPath.AddArc(X + RectWidth - (CornerRadius * 2), Y, CornerRadius * 2, CornerRadius * 2, 270, 90);
            gfxPath.AddLine(X + RectWidth, Y + CornerRadius, X + RectWidth, Y + RectHeight - (CornerRadius * 2));
            gfxPath.AddArc(X + RectWidth - (CornerRadius * 2), Y + RectHeight - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 0, 90);
            gfxPath.AddLine(X + RectWidth - (CornerRadius * 2), Y + RectHeight, X + CornerRadius, Y + RectHeight);
            gfxPath.AddArc(X, Y + RectHeight - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 90, 90);
            gfxPath.AddLine(X, Y + RectHeight - (CornerRadius * 2), X, Y + CornerRadius);
            gfxPath.AddArc(X, Y, CornerRadius * 2, CornerRadius * 2, 180, 90);
            gfxPath.CloseFigure();
            */
            //gfxObj.DrawPath(penObj, gfxPath);
            //gfxPath.Dispose();


            return gfxPath;

        }

    }
}
